/**
 * 公共方法 汇总
 */
const extendsQ = {
    data() {
        return {
            // 稻类列表
            riceTypeList: [
                { label: "籼稻", value: 1 },
                { label: "粳稻", value: 2 },
                { label: "籼糯", value: 3 },
                { label: "籼糯", value: 4 }
            ],
            // 级别列表
            numberList: [
                { label: "一级", value: 1 },
                { label: "二级", value: 2 },
                { label: "三级", value: 3 },
                { label: "无级别", value: 4 }
            ],
            // 稻种列表
            seedRiceList: [],
            // 页面切换导航
            qualityItems: [
                {
                    name: '待检记录',
                    href: './quality-inspection.html',
                    code: 'djjd_t',
                },
                {
                    name: '已检记录',
                    href: './inspected-records.html',
                    code: 'yjjd_t',
                }
            ]
        }
    },
    methods: {
        /**
         * 获取 筛选 类别名称
         * @param { Array } arr  筛选数组
         * @param { * } val   筛选值
         * 默认返回第一项 label
         */
        GETTABLENAME(arr, val) {
            let valueData = []
            if (!Array.isArray(arr)) {
                return ""
            }
            if ((val || val === 0)) {
                valueData = arr.filter(item => {
                    return item.value == val
                })
            } else {
                return ""
            }
            return valueData?.length ? valueData[0].label : "";
        },

        /**
         * 获取 名字
         * @param { * } val  作为寻找的值
         * @param { Array } arr 寻找数组
         * @param { * } key 数组中作为对比的 key
         * @param { * } valKey 找到的 名字 字段
         */
        getSeedRiceName(val, arr, key, valKey) {
            if (valKey == "variety_name") {
                let name = ""
                arr[0].children.forEach(item => {
                    if (item?.children) {
                        if (name === "") {
                            name = item?.children.find(el => {
                                return el[key] == val
                            })?.[valKey] || ""
                        }
                    }
                })
                if (name === "") {
                    arr[1].children.forEach(item => {
                        if (name === "") {
                            name = item?.children.find(el => {
                                return el[key] == val
                            })?.[valKey] || ""
                        }
                    })
                }
                return name
            } else {
                if (val == "" || !arr?.length) {
                    return ""
                }
                return arr.find(item => {
                    return item[key] == val
                })?.[valKey] || ""
            }
            
        },
        /**
         * 获取所属稻种 下拉列表
         */
        getSeedRiceOptions() {
            let params = { "variety_name": "" }
            tpHttp.post(this, "action=com.top.riceInstituteQuality.examineApprove.querySeedRice", { param: JSON.stringify(params) }, (data, code, message) => {
                if (code == 0 && data && JSON.stringify(data) != "{}") {
                    let arr = [{
                        variety_name: "国省",
                        id: "country",
                        children: []
                    },{
                        variety_name: "省审",
                        id: "province",
                        children: []
                    }]
                    if (data.country?.length) {
                        data.country.forEach((item,index) => {
                            let obj = {
                                variety_name: item.variety_type,
                                id: "country" + index,
                                children: []
                            }
                            if (item.value?.length) {
                                obj.children = item.value
                            }
                            arr[0].children.push(obj)
                        })
                    }
                    if (data.province?.length) {
                        data.province.forEach((item,index) => {
                            let obj = {
                                variety_name: item.variety_type,
                                id: "country" + index,
                                children: []
                            }
                            if (item.value?.length) {
                                obj.children = item.value
                            }
                            arr[1].children.push(obj)
                        })
                    }
                    this.seedRiceList = arr;
                } else {
                    this.seedRiceList = [];
                }
            })
        },
    },
    mounted() {
        this.getSeedRiceOptions();
    }
}